package com.example.demo.repository;

import com.example.demo.entity.Goods;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface GoodsRepository extends JpaRepository<Goods, Long>, JpaSpecificationExecutor<Goods> {


    /**
     * 功能描述: 扣除库存
     * @param: [goodsId, quantity]
     * @author: liujin
     * @date: 2020/12/18 15:10
     */
    @Modifying
    @Query("update Goods g set g.stock = g.stock - :quantity where g.id = :goodsId and g.stock >= :quantity")
    int reduceStock(@Param("goodsId") Long goodsId, @Param("quantity") Integer quantity);


    /**
     * 功能描述: 加库存
     * @param: [goodsId, quantity]
     * @author: liujin
     * @date: 2020/12/18 15:10
     */
    @Modifying
    @Query("update Goods g set g.stock = g.stock + :quantity where g.id = :goodsId")
    int addStock(@Param("goodsId") Long goodsId, @Param("quantity") Integer quantity);
}